﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using CoreFrame.Business;
using MarchEstate.Manager.Dao.Costform;
using CoreFrame.Core.Aop;
using CoreFrame.Core.Transaction;

namespace MarchEstate.Manager.ObjectModel.Costform
{
    public class MeterHead : AbstractMeterHead
    {
        private CostformDao formDao = null;

        public MeterHead()
        {

        }
        public override void InitDao()
        {
            base.InitDao();
            formDao = NewDao<CostformDao>();
        }


        public override bool Save()
        {
            if (MeterHeadId == null)
                formDao.AddMaterHeadform(this);
            return true;
        }

        public override bool Delete(string _meterHeadId)
        {
            return formDao.DeleteMeterHead(_meterHeadId);
        }

        public override List<AbstractMeterHead> GetMeterHeadList(int meterType, int meterType2, int _completeFlag, DateTime bdate, DateTime edate,CoreFrame.Core.Orm.PageInfo page)
        {
            return formDao.GetMeterHeadList(meterType, meterType2, _completeFlag, bdate, edate, page);
        }
        [AOP(typeof(AopTransaction))]
        private void savemakeform(int meterType, int meterType2, int getEmpId, int empId, List<AbstractMeter> meterlist, string BillNo)
        {
            this.MeterHeadId = null;
            this.BillNo = BillNo;// DateTime.Now.ToString("yyyyMMddHHmmss") + "01";
            this.MeterType = meterType;
            this.MeterType2 = meterType2;
            this.MeterDate = DateTime.Now;
            this.CompleteFlag = 0;
            this.GetEmpId = getEmpId;
            this.GetEmpName = CommonData.GetData("EmpName", getEmpId).ToString();
            this.EmpId = empId;
            this.EmpName = CommonData.GetData("EmpName", empId).ToString();
            this.Save();
            AbstractMeterOrder order = NewObject<AbstractMeterOrder>();
            for (int i = 0; i < meterlist.Count; i++)
            {
                AbstractMeterOrder _order = (AbstractMeterOrder)order.Clone();
                _order.MeterHeadId = this.MeterHeadId;
                _order.MeterId = meterlist[i].MeterId;
                _order.OwnerId = meterlist[i].OwnerId;
                _order.OwnerName = meterlist[i].OwnerName;
                _order.SrccId = meterlist[i].SrccId;
                _order.SrccName = meterlist[i].SrccName;
                _order.LastMeterDate = meterlist[i].LastMeterDate;
                _order.ThisMeterDate = DateTime.Now;
                _order.LastNum = meterlist[i].LastNum;
                _order.ThisNum = 0;
                _order.MeterNum = 0;
                _order.Thiscost = 0;
                _order.Save();
            }
        }
        [AOP(typeof(AopTransaction))]
        public override void BatchAddMakeForm(int meterType, int meterType2, int getEmpId, int empId)
        {
            AbstractMeter meter = NewObject<AbstractMeter>();
            List<AbstractMeter> meterlist = meter.GetMeterList(meterType, meterType2);
            if (meterlist.Count > 0)
            {
                if (meterType == 1)//住宅要根据楼宇生成表单
                {
                    List<string[]> list = new List<string[]>();
                    for (int i = 0; i < meterlist.Count; i++)
                    {
                        if (list.FindIndex(x => (x[0] == meterlist[i].AreaCode && x[1] == meterlist[i].BuildingCode)) == -1)
                        {
                            list.Add(new string[] { meterlist[i].AreaCode, meterlist[i].BuildingCode });
                        }
                    }

                    for (int i = 0; i < list.Count; i++)
                    {
                        List<AbstractMeter> meterlist1 = meterlist.FindAll(x => (x.AreaCode == list[i][0] && x.BuildingCode == list[i][1]));
                        if (meterlist1.Count > 0)
                        {
                            string no = DateTime.Now.ToString("yyyyMMddHHmmss") + (i.ToString().Length == 1 ? ("0" + i++) : i.ToString());
                            savemakeform(meterType, meterType2, getEmpId, empId, meterlist1, no);
                        }
                    }
                }
                else
                {
                    savemakeform(meterType, meterType2, getEmpId, empId, meterlist, DateTime.Now.ToString("yyyyMMddHHmmss") + "01");
                }
            }
            else
            {
                throw new Exception("没有可用的用表提供生成表单!");
            }
        }

        public override List<AbstractMeterHead> GetMeterHeadList(int meterType, int meterType2, int _completeFlag)
        {
            if (meterType == -1 && meterType2 == -1)
                return formDao.GetMeterHeadList(_completeFlag);
            else
                return formDao.GetMeterHeadList(meterType, meterType2, _completeFlag);
        }
        [AOP(typeof(AopTransaction))]
        public override bool AuditingMeterform(string _meterHeadId)
        {
            //判断是否还有明细没有填写完抄表数据
            //1.写入到WY_ItemFeeHead
            //2.写入到WY_ItemFeeOrder
            //3.写入到WY_OSRCCItemRelation


            if (formDao.CheckrecordMeterorder(_meterHeadId))
            {
                throw new Exception("您的表单没有录完，请先录完所有表单明细的本次数量再一起审核！");
            }

            AbstractMeterOrder order = NewObject<AbstractMeterOrder>();
            AbstractMeter meter = NewObject<AbstractMeter>();
            AbstractServiceItem item = NewObject<AbstractServiceItem>();
            AbstractItemFeeHead feehead = NewObject<AbstractItemFeeHead>();
            AbstractItemFeeOrder feeorder = NewObject<AbstractItemFeeOrder>();
            AbstractOSRCCItemRelation srcc = NewObject<AbstractOSRCCItemRelation>();


            List<AbstractMeterOrder> orderlist = order.GetMeterOrderList(_meterHeadId);
            for (int i = 0; i < orderlist.Count; i++)
            {
                AbstractMeter _meter = meter.GetMeter(orderlist[i].MeterId);
                AbstractServiceItem _item = item.GetServiceItem(_meter.ItemCode);

                AbstractItemFeeHead _feehead = (AbstractItemFeeHead)feehead.Clone();
                _feehead.HeadId = null;
                _feehead.OwnerId = orderlist[i].OwnerId;
                _feehead.OwnerName = orderlist[i].OwnerName;
                _feehead.SrccId = orderlist[i].SrccId;
                _feehead.SrccSonId = orderlist[i].MeterId;
                _feehead.ItemCode = _item.Code;
                _feehead.ItemName = _item.ItemName;
                _feehead.Lastmaturity = orderlist[i].LastMeterDate;
                _feehead.Thismaturity = orderlist[i].ThisMeterDate;
                _feehead.DosageNum = orderlist[i].MeterNum;
                _feehead.Price = _item.Price;
                _feehead.CostFee = orderlist[i].Thiscost;
                _feehead.LateFee = 0;//?
                _feehead.save();

                AbstractItemFeeOrder _feeorder = (AbstractItemFeeOrder)feeorder.Clone();
                _feeorder.OrderID = null;
                _feeorder.HeadId = _feehead.HeadId;
                _feeorder.OwnerId = _feehead.OwnerId;
                _feeorder.OwnerName = _feehead.OwnerName;
                _feeorder.SrccId = _feehead.SrccId;
                _feeorder.SrccSonId = _feehead.SrccSonId;
                _feeorder.ItemCode = _feehead.ItemCode;
                _feeorder.ItemName = _feehead.ItemName;
                _feeorder.StatCode = _item.StatCode;
                _feeorder.OrderDate = DateTime.Now;
                _feeorder.TotalFee = _feehead.CostFee;
                _feeorder.Save();

                srcc.UpdateThisFee(_feehead.SrccId, _feehead.ItemCode, _feehead.Thismaturity, _feehead.CostFee, _feehead.LateFee);
            }

            UpdateCompleteFlag(_meterHeadId);

            return true;
        }

        public override bool UpdateCompleteFlag(string _meterHeadId)
        {
            return formDao.UpdateCompleteFlag(_meterHeadId);
        }
    }
}
